package com.zhengtoudai.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhengtoudai.R;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.ui.bean.BorrowModel;
import com.zhengtoudai.util.Tool;
import com.zhengtoudai.view.MyProgressBar;
import com.zhengtoudai.view.recyclerview.RecyclerViewAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/8/29 16:47
 */
public class BorrowAdapter extends RecyclerViewAdapter<BorrowAdapter.ViewHolder, BorrowModel.BorrowBean.BorrowList> {

    private int progress;
    private Fragment mFragment;

    public BorrowAdapter(Fragment fragment) {
        mFragment = fragment;
    }

    @Override
    public ViewHolder mOnCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_project, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void mOnBindViewHolder(ViewHolder holder, int position) {
        holder.mTvProjectDeadlineTxt.setText("项目期限");
        holder.mTvTitle.setText(data.get(position).getBorrowTitle());
        holder.mTvYearRate.setText(data.get(position).getRate());
        holder.mTvProjectDeadline.setText(data.get(position).getBorrowTimeLimit());
        holder.mTvTimeStyle.setText(data.get(position).getIsDay());
        holder.mTvProjectPrice.setText(data.get(position).getMinAmount());

        if (data.get(position).getTenderProgressRate() != "" || (int) Tool.changedoublenopoint(data.get(position).getTenderProgressRate()) != 0) {
            progress = Integer.valueOf((int) Tool.changedoublenopoint(data.get(position).getTenderProgressRate()));
        }

        holder.mMyProgressbar.setProgress(progress);
        Glide.with(mFragment)
                .load(Config.URL+data.get(position).getImgUrl())
                .error(R.mipmap.tupian)
                .placeholder(R.mipmap.tupian)
                .into(holder.mIvImg);

        String status=data.get(position).getBorrowStatus();
        int backgroundResId;
        String btnText;
         if (status.equals("8")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="满标审核";
        }else if (status.equals("9")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="审核通过";
        }else if (status.equals("10")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="审核未通过";
        }else if (status.equals("11")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="还款中";
        }else if (status.equals("12")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="还款完成";
        }else if (status.equals("13")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="逾期";
        }else if (status.equals("14")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="自己取消";
        }else if (status.equals("15")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="平台取消";
        }else if (status.equals("16")){
            backgroundResId=R.drawable.shape_gray_button;
            btnText="已流标";
        }else{  //status.equals("6")
            backgroundResId=R.drawable.shape_red_button;
            btnText="立即投资";
        }
        holder.mBtnBuy.setBackgroundResource(backgroundResId);
        holder.mBtnBuy.setText(btnText);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_img)
        ImageView mIvImg;
        @Bind(R.id.tv_title)
        TextView mTvTitle;
        //        @Bind(R.id.tv_number_of_periods)
//        TextView mTvNumberOfPeriods;
        @Bind(R.id.tv_year_rate)
        TextView mTvYearRate;
        @Bind(R.id.tv_year_rate_txt)
        TextView mTvYearRateTxt;
        @Bind(R.id.tv_project_deadline)
        TextView mTvProjectDeadline;
        @Bind(R.id.tv_time_style)
        TextView mTvTimeStyle;
        @Bind(R.id.tv_project_deadline_txt)
        TextView mTvProjectDeadlineTxt;
        @Bind(R.id.tv_project_price_txt)
        TextView mTvProjectPriceTxt;
        @Bind(R.id.tv_project_price)
        TextView mTvProjectPrice;
        @Bind(R.id.tv_project_price_unit)
        TextView mTvProjectPriceUnit;
        @Bind(R.id.btn_buy)
        Button mBtnBuy;
        @Bind(R.id.my_progressbar)
        MyProgressBar mMyProgressbar;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
